package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        String username = (String) para.get("username");
        String password = (String) para.get("password");
        User user = userService.Login(username, password);
        HashMap<String, Object> hs = new HashMap<>();
        String token = null;
        if (user != null) {
            token = TokenUtil.sign(user);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 403));
        }
        hs.put("token", token);
        hs.put("data", user);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data", "data");
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //分页查询
    @RequestMapping(value = "/findUserByPage",method = RequestMethod.GET)
//    @ResponseBody
    public String UserPage(@RequestParam Integer pageNum,Integer pageSize,String query) throws JsonProcessingException {
        System.out.println(pageNum+pageSize+query);
        //json方法
        ObjectMapper objectMapper=new ObjectMapper();
        Map<String,Object> modelMap=new HashMap<>();
        //获取分页参数
//        Integer num = (Integer)map.get("pageNum");
//        Integer size = (Integer)map.get("pageSize");
//        String query = (String) map.get("query");
        //设置分页信息，要显示的页数和条数
        PageHelper.startPage(pageNum,pageSize);
        //执行查询
        List<User> userList=userService.queryAllUser(query);
        if (userList.size()>0){
            //pageInfo是pageHelper中的内置的分页信息类
            PageInfo<User> info=new PageInfo(userList);
            //将所有数据存到集合中

            modelMap.put("data",info);
            modelMap.put("meta",new Constance("获取数据成功",200));
        }else{
            modelMap.put("meta",new Constance("获取数据失败",404));
        }
        //将集合转换为json串响应给前台
        return objectMapper.writeValueAsString(modelMap);
    }

    @RequestMapping(value = "/updataUser", method = RequestMethod.PUT)
    public String modifyUser(@RequestBody User user) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(userService.updateUser(user)){
            hs.put("meta", new Constance("修改成功", 200));
        }else{
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteUserById", method = RequestMethod.DELETE)
    public String deleteUser(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(userService.deleteUserById(id)){
            hs.put("meta", new Constance("删除成功", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public String addUser(@RequestBody User user)throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        if(userService.addUser(user)){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value="/findUserById",method = RequestMethod.GET)
    public String findUserById(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        User user = userService.findUserById(id);
        if(user!=null){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        hs.put("data", user);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    @RequestMapping(value = "/deleteUsersById", method = RequestMethod.POST)
    public String deleteUsers(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer num = userService.deleteUsers(ids.get("ids"));
        if(num>0){
            hs.put("meta", new Constance("删除成功！"+num+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

}